/*
 * @Author: dengzewei
 * @Date: 2025-04-15 18:04:28
 * @LastEditors: dengzewei
 * @LastEditTime: 2025-04-15 18:20:34
 * @FilePath: /chrome_new/aidso/token.js
 * @Description: 
 * 
 * Copyright (c) 2025 by 抖查查, All Rights Reserved. 
 */
// 添加日志来确认脚本加载
// Token 处理相关功能
(function() {
    function isTargetPage() {
        const currentUrl = window.location.href;
        return currentUrl.includes('.aidso.com');
      }
      let lastToken = null;
      function initBtn() {
        if (!isTargetPage()) {
            // console.log('当前页面不是目标页面，不注入授权按钮');
            return;
        }
        // 检查是否已经注入过
        if (document.querySelector('#DsoChrome')) {
            // console.log('授权按钮已经注入过，跳过注入');
            return;
        }
        // console.log('注入授权按钮');
        const button = document.createElement('div');
        button.id = 'DsoChrome';
        button.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: -9999;
            
        `;
        document.body.appendChild(button);
      }
      function initToken() {
        const token = localStorage.getItem('token') || '';
        if ( token !== lastToken) {
          sendToken(token);
        }
      }
    //   发送 token
      function sendToken(token) {
        chrome.runtime.sendMessage({
            action: 'setToken',
            token: token
        });
        lastToken = token;
      }
    //   开始定期检查
    setInterval(() => {
        initBtn();
        initToken();
    }, 1000);
    
})();
